<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="query.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="query.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="query.cardStatus">
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="doSearch">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/cardAdd')">添加月卡</el-button>
      <!-- 方案1：根据选中的月卡的长度来判断是否 使能 按钮的点击功能 -->
      <!-- <el-button :disabled="ids.length === 0" @click="deleteCarCard(ids)">批量删除</el-button> -->
      <!-- 方案2：按钮点击功能不变，在删除的时候给用户提示要勾选月卡 -->
      <el-button @click="deleteCarCard(ids)">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div v-loading="loading" class="table">
      <el-table style="width: 100%" :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="formatStatus" />
        <!-- <el-table-column label="状态">
          <template #default="{ row }">
            {{ row.cardStatus | getStatusText }}
          </template>
        </el-table-column> -->
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{ row }">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="$router.push('/cardAdd?id=' + row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleteCarCard([row.id])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total,prev,pager,next"
        :total="total"
        :page-size="query.pageSize"
        :current-page="query.page"
        @current-change="onCurrentChange"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCarCardListAPI, deleteCarCardAPI } from '@/api/car'
import { CARD_STATUS_2_TEXT } from '@/constants/map'

export default {
  filters: {
    getStatusText(status) {
      return status === 0 ? '可用' : '已过期'
    }
  },
  data() {
    return {
      ids: [],
      loading: false,
      list: [],
      query: {
        page: 1,
        pageSize: 5,
        // 以下三个字段是非必填
        carNumber: null,
        personName: null,
        cardStatus: null
      },
      total: 0,
      statusOptions: [
        {
          value: null,
          label: '全部'
        },
        {
          value: 0,
          label: '可用'
        },
        {
          value: 1,
          label: '已过期'
        }
      ]
    }
  },
  created() {
    // this.ids = []
    this.getListData()
  },
  methods: {
    async getListData() {
      this.loading = true
      const res = await getCarCardListAPI(this.query)
      this.loading = false
      this.list = res.data.rows
      this.total = res.data.total
    },
    formatStatus(row) {
      return CARD_STATUS_2_TEXT[row.cardStatus]
    },
    // 当前页改变时回调该函数
    onCurrentChange(page) {
      // 改变当前页，然后获取新数据，渲染表格
      this.query.page = page
      this.getListData()
    },
    // 执行搜索
    doSearch() {
      // 每次搜索/查询的时候，都应该从第一页开始
      this.query.page = 1
      this.getListData()
    },
    // 删除月卡 ==> 敏感操作 ==> 用户二次确认
    deleteCarCard(ids = []) {
      // 对ids进行非空判断
      if (ids.length === 0) {
        this.$message.warning('请选择要删除的月卡先')
        return
      }
      this.$confirm('此操作将永久删除月卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // console.log('确认删除')
          // 1. 调接口
          // 2. 提示用户
          // 3. 重新渲染当前页（如果当前页有且只有一条数据，那么要渲染上一页）
          return deleteCarCardAPI(ids)
        })
        .then(() => {
          this.$message.success('删除成功')
          // todo: 可以加一个判断，判断是否需为当前页的唯一一条数据，如果是，那么页码要--，如果已经是第一页了，就不能--了
          if (this.list.length === 1 && this.query.page > 1) {
            this.query.page--
          }
          // 重新渲染
          this.getListData()
        })
        .catch(() => {})
    },
    handleSelectionChange(selection = []) {
      this.ids = selection.map(item => item.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
